//
//  LXLaunch.m
//  duoduo
//
//  Created by wanghao on 16/10/11.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXLaunch.h"
#import "LXTableBarController.h"
#import "DCPicScrollView.h"

@interface LXLaunch ()<UIScrollViewDelegate>
{
    int cout ;
    NSTimer *_cjTimer;

    //轮播图的数据
    NSArray *cycleImageArr;
    DCPicScrollView  *picView;
    UIScrollView * scro;
    UIPageControl * PageControl;
}

//@property (weak, nonatomic) IBOutlet UIImageView *durationImage;

@end

@implementation LXLaunch

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    cout = 0;
    [self getJsonData];
    cycleImageArr = @[@"beijing",@"nav1.jpg",@"nav2.jpg"];
    UIButton * button = [[UIButton alloc]init];
    button.frame = CGRectMake(ScreenWidth-100, 30, 90, 40);
    [button setTitle:@"点击跳过" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    button.backgroundColor = [UIColor whiteColor];
    button.layer.cornerRadius = 8;
    button.layer.masksToBounds = YES;
    button.alpha = 0.5;
    [button addTarget:self action:@selector(clickToSprite:) forControlEvents:UIControlEventTouchUpInside];

    [self.view bringSubviewToFront:button];
    [self getinitUI];
    //[self getTableHeaderView:cycleImageArr];
//    _durationImage.userInteractionEnabled = YES;
//    _durationImage.image = [UIImage imageNamed:@"beijing"];
//    _cjTimer =   [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(overDuration:) userInfo:nil repeats:YES];
}

-(void)getJsonData{

//    NSDictionary *dic =@{@"code":@"D00000006"};
//    [[GYHTTPNetWorkManager shareNetWorking]POST:bax_patch_get parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
//        WH_DLog(@"启动页结果＝＝＝%@",responseObject);
//        if (resultJudge) {
//            NSString*imageUrl = responseObject[@"data"][@"desc_arr"][0][1];
//            [_durationImage sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"qidongye.png"]];
//        }
//    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//
//    }];

}

- (void)getinitUI
{
    NSArray * imageArray = @[@"nav1.jpg",@"nav2.jpg",@"beijing"];
    scro = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    scro.contentSize = CGSizeMake(ScreenWidth*imageArray.count, 0);
    //分页
    scro.pagingEnabled = YES;
    scro.delegate = self;
    //隐藏滚动条
    scro.showsVerticalScrollIndicator = NO;
    scro.showsHorizontalScrollIndicator = NO;
    //关闭弹簧
    scro.bounces = NO;
    [self.view addSubview:scro];


    //添加图片

    NSArray * array1  = @[@"firstPage",@"secondPage",@"thirdPage"];
    for (NSInteger i=0; i<imageArray.count; i++) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, ScreenHeight)];
        imageView.image = [UIImage imageNamed:imageArray[i]];
        [scro addSubview:imageView];

        imageView.userInteractionEnabled = YES;


        imageView.tag = i;
        
        if (i==0) {
            UIView * view = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, ScreenHeight)];
            view.backgroundColor = [UIColor blackColor];
            UIImageView * upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth*i+60, 200, ScreenWidth-120, 30)];
            upImageView.image = [UIImage imageNamed:array1[i]];
            [view addSubview:upImageView];
            [imageView addSubview:view];

            UIImageView * ido = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth*i+100, 260, ScreenWidth-200, 40)];
            ido.image = [UIImage imageNamed:@"ido"];

            [view addSubview:ido];

            UIImageView * right = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth*i+ScreenWidth-200, 340, 190, 40)];
            right.image = [UIImage imageNamed:@"info"];

            [view addSubview:right];

            view.alpha = 0.3;
        }else if(i==1) {
            UIView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
            view.backgroundColor = [UIColor blackColor];
            UIImageView * upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 200, ScreenWidth-120, 60)];
            upImageView.image = [UIImage imageNamed:array1[i]];
            [view addSubview:upImageView];
            [imageView addSubview:view];

            UIImageView * ido = [[UIImageView alloc]initWithFrame:CGRectMake(100, 290, ScreenWidth-200, 40)];
            ido.image = [UIImage imageNamed:@"ido"];

            [view addSubview:ido];

            view.alpha = 0.3;
        }else{
            UIView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
            view.backgroundColor = [UIColor blackColor];
            UIImageView * upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 100, ScreenWidth-120, 90)];
            upImageView.image = [UIImage imageNamed:array1[i]];
            [view addSubview:upImageView];
            [imageView addSubview:view];

            UIImageView * ido = [[UIImageView alloc]initWithFrame:CGRectMake(100, 210, ScreenWidth-200, 40)];
            ido.image = [UIImage imageNamed:@"ido"];

            [view addSubview:ido];

            view.alpha = 0.3;
        }


        if (i==imageArray.count-1) {
            UIButton * button = [[UIButton alloc]init];
            button.frame = CGRectMake(ScreenWidth-100, 30, 90, 40);
            [button setTitle:@"点击跳过" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:16];
            button.backgroundColor = [UIColor whiteColor];
            button.layer.cornerRadius = 8;
            button.layer.masksToBounds = YES;
            button.alpha = 0.5;
            [button addTarget:self action:@selector(clickToSprite:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:button];
        }
        //[scro bringSubviewToFront:button];

    }

    

    PageControl = [[UIPageControl alloc]init];

    CGFloat controlW = 170;
    CGFloat controlH = 20;
    CGFloat controlX = CGRectGetMidX(self.view.frame) - controlW / 2;
    CGFloat controlY = CGRectGetMaxY(self.view.frame) - 38;
    PageControl.frame = CGRectMake(controlX, controlY, controlW, controlH);
    PageControl.numberOfPages = 3;
    PageControl.currentPage = 0;
    PageControl.pageIndicatorTintColor = [UIColor colorWithRed:234./255. green:234./255. blue:234./255. alpha:1.];
    PageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:125./255. green:153./255. blue:255./255. alpha:1.];
PageControl.tag = 15;

    [self.view addSubview:PageControl];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSUInteger index = scrollView.contentOffset.x / CGRectGetWidth(self.view.frame);
    [self.view viewWithTag:15].hidden = (index != cycleImageArr.count - 1);

    UIPageControl* control =[self.view viewWithTag:15];
    control.currentPage = index;

}

#pragma mark- TableViewHead
-(void)getTableHeaderView:(NSArray *)arr
{
    picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) WithImageUrls:arr];
    picView.AutoScrollDelay = 0;
    [self.view addSubview:picView];

}

-(void)clickToSprite:(UIButton *)button
{
    UIScrollView * scro = (UIScrollView *)button.superview;
    [UIView animateWithDuration:2 animations:^{
        scro.alpha = 0;
    } completion:^(BOOL finished) {
        scro.hidden = YES;
    }];

    appD.window.rootViewController = [LXTableBarController new];
    
    
}

-(void)overDuration:(NSTimer*)timer{

    cout++;
    if (cout==3) {
        [self gameOver];
    }
}

//- (void)clickToSprite:(UIButton *)sender {
//
//    UIButton * bt =(UIButton*)sender;
//    bt.selected =YES;
//    [self gameOver];
//}

-(void)gameOver
{
    [_cjTimer invalidate];
    _cjTimer = nil;

    self.view.window.rootViewController = [[LXTableBarController alloc] init];
}


@end
